package com.example.ekupiandroid;

import java.util.Date;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import com.example.classes.Artikal;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.example.ekupiandroid.KreiranjeKorpe;

public class KorisnickaKorpa extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_korisnicka_korpa);
		
		final String korisnik =  getIntent().getExtras().getString("korisnik");
		
		
		final ArrayList<Artikal> artikliUKorpi = KreiranjeKorpe.getArtikliUKorpi();
		
		  runOnUiThread(new Runnable() {
			     @Override
			     public void run() {
			    	 double ukupnaCijena = 0;
			    	 for (Artikal a : artikliUKorpi) {
			    		 ukupnaCijena += a.getKolicina()*a.getCijena();
			    	 }
			    	 final EditText cijena = (EditText) findViewById(R.id.editText1);
			    	 cijena.setText(""+ukupnaCijena);
			    }
			});
		
		Button zavrsi = (Button) findViewById(R.id.button1);
		
		
		zavrsi.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Thread t = new Thread (new Runnable() {
					
					public void run() {
				
				
				HttpClient client = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
                final HttpResponse response;
                final JSONObject jason = new JSONObject();

                try {
                	
                	final JSONObject jsonObj = new JSONObject(korisnik);
                	Date datum = new Date();
                    HttpPut put = new HttpPut("http://10.0.2.2:8080/eKupovina/webservices/narudzbenice/narudzbenica/nova");
                    //jason.put("idKorisnika", idKorisnika);
                    jason.put("datum",datum);
                    jason.put("korisnik", jsonObj);
//                    jason.put("artikal", KreiranjeKorpe.getArtikliUKorpi());
                    
                    StringEntity se = new StringEntity(jason.toString());  
                    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                    put.setEntity(se);
                    response = client.execute(put);
                    int idNarudzbenice = 0;
                    if(response!=null){
//                      InputStream in = response.getEntity().getContent(); //Get the data in the entity
                    	
                        final String responseStr = EntityUtils.toString(response.getEntity());
                        idNarudzbenice = Integer.parseInt(responseStr);
                        
                    }
                    
                    
                    for (Artikal art : artikliUKorpi) {
                    	 HttpPut putStavke = new HttpPut("http://10.0.2.2:8080/eKupovina/webservices/stavke/stavka/dodaj");
//                    	 final JSONObject jsonObjStavke = new JSONObject(getIntent().getStringExtra("korisnik"));
                    	 final JSONObject jsonObjStavke = new JSONObject();
                    	 jsonObjStavke.put("kolicina",art.getKolicina());
                    	 jsonObjStavke.put("idNarudzbenice", idNarudzbenice);
                    	 jsonObjStavke.put("idArtikla",art.getIdArtikal());
                         StringEntity seStavke = new StringEntity(jsonObjStavke.toString());  
                         seStavke.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                         putStavke.setEntity(seStavke);
                         try {
							HttpResponse responseStavke = client.execute(putStavke);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
                         
                       
                    }
                    
                    
                    

                } catch(Exception e) {
                    e.printStackTrace();
                    //createDialog("Error", "Cannot Estabilish Connection");
                }
						
					}
				});
				t.start();
			}
		});
		
		
		ArrayAdapter<Artikal> adapter=new ArrayAdapter<Artikal>(this,
                android.R.layout.simple_list_item_1, artikliUKorpi);
		
		//ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
               // android.R.layout.simple_list_item_1, nizNazivMarketa);
		
		final ListView lista = (ListView) findViewById(R.id.listView1);
		lista.setAdapter(adapter);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.korisnicka_korpa, menu);
		return true;
	}

}
